package com.uls.ldcm.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.ldcm.domain.vo.SalaryCountVo;
import com.uls.ldcm.service.ILaborersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ldcm.domain.Salaryrecords;
import com.uls.ldcm.service.ISalaryrecordsService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 薪资记录Controller
 * 
 * @author gw
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/ldcm/salaryrecords")
public class SalaryrecordsController extends BaseController
{
    @Autowired
    private ISalaryrecordsService salaryrecordsService;

    @Autowired
    private ILaborersService laborersService;

    /**
     * 查询薪资记录列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryrecords:list')")
    @GetMapping("/list")
    public TableDataInfo list(Salaryrecords salaryrecords)
    {
        startPage();
        List<Salaryrecords> list = salaryrecordsService.selectSalaryrecordsList(salaryrecords);
        for (Salaryrecords salaryrecords1:list){
            salaryrecords1.setName(laborersService.selectLaborersById(salaryrecords1.getLaborerId()).getName());
        }
        return getDataTable(list);
    }

    /**
     * 导出薪资记录列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryrecords:export')")
    @Log(title = "薪资记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Salaryrecords salaryrecords)
    {
        List<Salaryrecords> list = salaryrecordsService.selectSalaryrecordsList(salaryrecords);
        ExcelUtil<Salaryrecords> util = new ExcelUtil<Salaryrecords>(Salaryrecords.class);
        util.exportExcel(response, list, "薪资记录数据");
    }

    /**
     * 获取薪资记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryrecords:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(salaryrecordsService.selectSalaryrecordsByRecordId(recordId));
    }

    /**
     * 新增薪资记录
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryrecords:add')")
    @Log(title = "薪资记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Salaryrecords salaryrecords)
    {
        return toAjax(salaryrecordsService.insertSalaryrecords(salaryrecords));
    }

    /**
     * 修改薪资记录
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryrecords:edit')")
    @Log(title = "薪资记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Salaryrecords salaryrecords)
    {
        return toAjax(salaryrecordsService.updateSalaryrecords(salaryrecords));
    }

    /**
     * 删除薪资记录
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryrecords:remove')")
    @Log(title = "薪资记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(salaryrecordsService.deleteSalaryrecordsByRecordIds(recordIds));
    }

    /**
     * 总工资报表统计
     */

    @GetMapping("/salaryCount")
    public AjaxResult salaryCount()
    {
        List<SalaryCountVo> salaryCountVos = salaryrecordsService.selectSalaryCountVoList();
        List<SalaryCountVo> salaryCountVoList=new ArrayList<>();
        for (SalaryCountVo salaryCountVo:salaryCountVos){
            SalaryCountVo salaryCountVo1 = new SalaryCountVo();
            salaryCountVo1.setYear(salaryCountVo.getYear());
            salaryCountVo1.setMonth(salaryCountVo.getMonth());
            salaryCountVo1.setTotalSalary(salaryCountVo.getTotalSalary());
            salaryCountVoList.add(salaryCountVo1);
        }
        return AjaxResult.success(salaryCountVoList);
    }
}
